<?php

namespace org\copperforest\oak\authorization;

class DAGSecurityContext extends SecurityContext
{

    protected $basicRoles = array( ); //indexado por el identificador 

    function __construct( $roles = null )
    {

        foreach ( $roles as $k => $array )
                $this->basicRoles[ ( int ) $k ] = $array;
    }

    public function serialize()
    {//tengo que serializar el id!!!!!!!!!!!!!!!!!! para poder deserializarlo
        $partes = array( );

        ksort( $this->basicRoles );

        foreach ( $this->basicRoles as $id => $roles ) {
            sort( $roles );

            $partes[ ] = $id . '#' . implode( '#', $roles );
        }

        return implode( '@', $partes );
    }

    public function unserialize( $string )
    {

        $partes = explode( '@', $string );

        foreach ( $partes as $p ) {
            $roles = explode( '#', $p );

            $num = count( $roles );
            $id = ( int ) $roles[ 0 ];

            $this->basicRoles[ $id ] = array( );

            for ( $i = 1; $i < $num; $i++ )
                    $this->basicRoles[ $id ][ ] = $roles[ $i ];
        }
    }

    function join( DAGSecurityContext $context )
    {
        //tengo que obtener los roles

        $roles = $this->basicRoles;

        foreach ( $context->basicRoles as $k => $array ) {
            if ( isset( $roles[ $k ] ) ) {
                foreach ( $array as $r )
                        if ( !in_array( $r, $roles[ $k ] ) ) $roles[ $k ] = $r;
            }
            else $roles[ $k ] = $array;
        }

        return new DAGSecurityContext( $roles );
    }

    function split()
    {
        /**
         * 
          $contexts=array();

          foreach($this->basicRoles as $id=>$roles)
          foreach($roles as $r)
          $contexts[]=new DAGSecurityContext(array($id=>$r));

          return $contexts;
         * 
         */
        if ( count( $this->basicRoles ) == 1 && count( end( $this->basicRoles ) ) == 1 )
                return array( $this );

        $serializations = array( );

        foreach ( $this->basicRoles as $id => $roles )
                foreach ( $roles as $r )
                    $serializations[ ] = ct\CT::$PDO->quote( $id . '#' . $r );

        $contexts = array( );


        $resultado = ct\CT::$PDO->query( 'SELECT context_id, serialization FROM ct_security_contexts WHERE serialization IN (' . implode( ',',
                        $serializations ) . ')' );


        foreach ( $resultado as $r ) {

            $obj = unserialize( $r[ 1 ] );
            $obj->id = $r[ 0 ];

            $contexts[ ] = $obj;

            unset( $serializations[ $r[ 1 ] ] );
        }


        if ( ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts (serializarion) VALUES (' . implode( ',',
                                $serializations ) . ')' ) ) {

            $id = ct\CT::$PDO->lastInsertId();

            foreach ( $serializations as $s ) {
                $obj = unserialize( $s );
                $obj->id = $id;

                $contexts[ ] = $obj;
                $id++;
            }
        }

        return $contexts;
    }

}

?>
